<template>
    <el-dialog @close="reset" title="角色编辑" v-model="visible">
        <el-form ref="roleAddForm" :model="formData" label-width="80px" :rules="rules">
            <el-form-item label="角色名称" prop="rolename">
                <el-input placeholder="请输入角色名称" v-model="formData.rolename" autocomplete="off" />
            </el-form-item>
            <el-form-item label="角色状态" prop="status">
                <el-radio-group v-model="formData.status">
                    <el-radio :value="1">启用</el-radio>
                    <el-radio :value="2">禁用</el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="角色权限">
                <el-tree :default-checked-keys="checkedKeys" ref="treeRef" style="max-width: 600px"
                    :data="menu.menuList" show-checkbox node-key="id" default-expand-all :props="defaultProps" />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="reset">取消</el-button>
                <el-button @click="submit" type="primary">
                    确认提交
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { roleAdd, roleUpdate } from '@/utils/api'
import { useStore } from 'vuex'
import { ElMessage } from 'element-plus';
const store = useStore()
const { dispatch, state: { menu } } = store
onMounted(() => {
    dispatch('menu/getMenuListAction')
})

const roleAddForm = ref(null)
const treeRef = ref(null)
const visible = ref(false)

const formData = ref({
    rolename: '',
    status: ''
})
const rules = ref({
    rolename: { required: true, message: '请输入角色名称', trigger: 'blur' }
})
const emit = defineEmits(['updateRoleList']);
const defaultProps = {
    children: 'children',
    label: 'title',
}
const checkedKeys = ref([])
// 接收待编辑菜单数据
const getEditData = (data) => {
    formData.value = { ...data }
    visible.value = true
    // 设置树形控件勾选项
    checkedKeys.value = data.permission.split(',')
}
// 重置数据
const reset = () => {
    // 关闭对话框
    visible.value = false
    // 重置表单
    roleAddForm.value.resetFields()
    // 重置树形控件勾选项
    treeRef.value.setCheckedKeys([])
    delete formData.value.id
}
const submit = () => {
    roleAddForm.value.validate(async valid => {
        if (valid) {
            const permission = treeRef.value.getCheckedKeys().join(',')
            const payload = { ...formData.value, permission }
            var res
            if (formData.value.id > 0) {
                res = await roleUpdate(payload)
            } else {
                res = await roleAdd(payload)
            }
            const { data } = res
            ElMessage({ message: data.msg, type: data.code === 200 ? 'success' : 'error' })
            if (data.code === 200) {
                // 更新角色列表: 通知父组件更新
                emit('updateRoleList')
                reset()
            }
        }
    })
}

defineExpose({
    visible, getEditData
})
</script>

<style></style>